﻿using MyShop;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 经典项目某某管理系统.Erp
{
    public partial class FrmCash : Form
    {
        public FrmCash()
        {
            InitializeComponent();
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }
        private void BindCtorl()
        {
            string sql = "select * from Salesman where Role ='导购员' ";
            DBHelper.PrepareSql(sql);
            DataTable dt = DBHelper.ExecQuery();
            DataRow dr = dt.NewRow();
            dr["SalesmanName"] = "请选择";
            dr["SalesmanID"] = "0";
            dt.Rows.InsertAt(dr, 0);
            this.cmbClass.DataSource = dt;
            this.cmbClass.ValueMember = "SalesmanID";
            this.cmbClass.DisplayMember = "SalesmanName";
        }
        private string GetNumber()
        {
            return DateTime.Now.ToString("yyyyMMddhhmmssfff");
        }
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void FrmCash_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void FrmCash_Load(object sender, EventArgs e)
        {
            BindCtorl();
            this.cmbClass.SelectedIndex = 0;
            this.label11.Text = GetNumber();
            this.PriceText.Text = "共: ￥0.00元";
            this.label5.Text = "商品数量: 0";
            this.label6.Text = "收银员: " + LoginCtl.SalesmamanName;
            this.textBox2.Text = "0.00";
            this.textBox3.Text = "0.00";
            this.textBox4.Text = "0.00";
        }

        private void UpdateStore()
        {
            double sumMoney = 0;
            int sumCount = 0;
            foreach (ListViewItem good in this.listView1.Items)
            {
                //MessageBox.Show(good.Index.ToString());
                sumMoney += int.Parse(good.SubItems[6].Text) * double.Parse(good.SubItems[5].Text);
                sumCount += int.Parse(good.SubItems[6].Text);
            }
            this.PriceText.Text = "共: " + sumMoney.ToString("F2") + "元";
            this.label5.Text = "商品数量: " + sumCount;
            this.textBox2.Text = sumMoney.ToString("F2");
            //this.textBox3.Text = "0.00";
            this.textBox4.Text = (double.Parse(this.textBox3.Text) - double.Parse(this.textBox2.Text)).ToString();
            this.textBox1.Text = "";
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //键盘事件
            int keyCode = e.KeyChar;
            if (keyCode != 13) { return; }
            foreach (ListViewItem good in this.listView1.Items)
            {
                if (this.textBox1.Text.Equals(good.Text))
                {
                    int goodCount = int.Parse(good.SubItems[6].Text);
                    goodCount++;
                    good.SubItems[6].Text = goodCount.ToString();
                    //更新界面其他控件
                    UpdateStore();
                    return;
                }
            }
            string sql = "select * from Goods inner join [Type] on Goods.TypeID = [Type].TypeID where BarCode = @BarCode";
            DBHelper.PrepareSql(sql);
            DBHelper.SetParameter("BarCode", this.textBox1.Text);
            DataTable dt = DBHelper.ExecQuery();
            //MessageBox.Show(dt.Rows.Count.ToString());
            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("没有找到商品");
                //this.T
                return;
            }
            ListViewItem it = new ListViewItem(dt.Rows[0]["BarCode"].ToString());
            it.SubItems.Add(dt.Rows[0]["GoodsName"].ToString());
            it.SubItems.Add(dt.Rows[0]["TypeName"].ToString());
            double salePrice = double.Parse(dt.Rows[0]["SalePrice"].ToString());
            double discount = double.Parse(dt.Rows[0]["Discount"].ToString());
            it.SubItems.Add(dt.Rows[0]["SalePrice"].ToString());
            it.SubItems.Add(dt.Rows[0]["Discount"].ToString());
            //it.SubItems.Add((double.Parse(dt.Rows[0]["SalePrice"].ToString()) * double.Parse(dt.Rows[0]["Discount"].ToString())).ToString("F2"));
            it.SubItems.Add((salePrice * discount).ToString("F2"));
            it.SubItems.Add("1");
            it.SubItems.Add(dt.Rows[0]["GoodsID"].ToString());
            this.listView1.Items.Add(it);
            UpdateStore();
        }
        private DateTime lastKeyPressTime = DateTime.MinValue;
        private void listView1_KeyPress(object sender, KeyPressEventArgs e)
        {
            var now = DateTime.Now;
            if ((now - lastKeyPressTime).TotalMilliseconds < 200) // 200ms 内忽略
                return;
            lastKeyPressTime = now;
            //MessageBox.Show(((int)e.KeyChar).ToString());
            if (this.listView1.SelectedItems.Count == 0)
            {
                return;
            }
            int keyCode = ((int)e.KeyChar);
            if (keyCode == 43) //+
            {
                int goodCount = Convert.ToInt32(this.listView1.SelectedItems[0].SubItems[6].Text);
                goodCount+=1;
                //MessageBox.Show(goodCount.ToString());
                this.listView1.SelectedItems[0].SubItems[6].Text = goodCount.ToString();
                //return;
            }
            if (keyCode == 45) //-
            {
                int goodCount = Convert.ToInt32(this.listView1.SelectedItems[0].SubItems[6].Text);
                if (goodCount == 1)
                {
                    return;
                }
                goodCount--;
                this.listView1.SelectedItems[0].SubItems[6].Text = goodCount.ToString();
            }
            if (keyCode == 8)//delete 删除
            {
                this.listView1.SelectedItems[0].Remove();
            }
            UpdateStore();
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            int keyCode = (int)e.KeyChar;
            if (keyCode != 13) {
                return;
            }
            this.textBox4.Text = (double.Parse(this.textBox3.Text) - double.Parse(this.textBox2.Text)).ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //账单
            string sql = "insert into Sales(ReceiptsCode,SalesDate,AllMoney,SalesmanID,CashierID) values(@ReceiptsCode, getdate(), @AllMoney, @SalesmanID, @CashierID) select @@identity";
            DBHelper.PrepareSql(sql);
            DBHelper.PrepareSql(sql); DBHelper.SetParameter("ReceiptsCode", this.label11.Text);
            DBHelper.SetParameter("AllMoney", this.textBox2.Text);
            DBHelper.SetParameter("SalesmanID", this.cmbClass.SelectedValue.ToString());
            DBHelper.SetParameter("CashierID", LoginCtl.SalesmamanID);
            //DBHelper.ExecNonQuery();
            int salesId = int.Parse(DBHelper.ExecScalar().ToString());
            //明细
            foreach (ListViewItem good in this.listView1.Items)
            {
                sql = "insert into SalesDetail(SalesID,GoodsID,Quantity,GoodsMoney) values(@SalesID,@GoodsID,@Quantity,@GoodsMoney)";
                DBHelper.PrepareSql(sql);
                DBHelper.SetParameter("SalesID", salesId);
                DBHelper.SetParameter("GoodsID", good.SubItems[7].Text);
                DBHelper.SetParameter("Quantity", good.SubItems[6].Text);
                DBHelper.SetParameter("GoodsMoney", good.SubItems[5].Text);
                DBHelper.ExecNonQuery();
            }
            MessageBox.Show("结算成功!");
            this.textBox1.Text = "";
            this.listView1.Items.Clear();
            BindCtorl();
        }
    }
}
